package com.btkjgc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.extend.ExceptionRecordModel;
import com.btkjgc.service.intf.TradeAbnormalGeneralService_Intf;

@Controller
public class TradeAbnormalGeneralController{
	static Logger logger = Logger
			.getLogger(TradeAbnormalGeneralController.class);
	
	@Autowired
	private TradeAbnormalGeneralService_Intf tradeAbnormalGeneralService;
	
	@RequestMapping(value = "trade_abnormal_general", method = {
			RequestMethod.GET, RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,HttpServletResponse response) {

		ModelAndView mv = null;
		Integer result = null;
		
		String task = request.getParameter("task");
		if ("company".equals(task)) {

			mv = new ModelAndView("trade_abnormal_company_general");
			result = companyListrecord(request, response,mv);
		} else if ("person".equals(task) || StringUtils.isEmpty(task)) {

			mv = new ModelAndView("trade_abnormal_person_general");
			result = personListrecord(request,response, mv);
		}

		if(result==1){
			return mv;
		}else {
			return null;
		}
	
	}
	
	@SuppressWarnings("unchecked")
	public Integer personListrecord(HttpServletRequest request,HttpServletResponse response,ModelAndView mv) {

		// 个人账户
		String tel = request.getParameter("tel");
		String begindate = request.getParameter("begindate");
		String enddate = request.getParameter("enddate");
		String status = request.getParameter("status");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "aut.tel", "like", tel };
		Object[] arr2 = { "ert.createdate", "d>", begindate };
		Object[] arr3 = { "ert.createdate", "d<", enddate };
		Object[] arr4 = { "ert.status", "=", status };
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		paramList.add(arr4);

		String sql = "SELECT\n"
				+ "	ert.*, eet.serialNum,\n"
				+ "	adut.username opername,\n"
				+ "	aut.NAME accountName,\n"
				+ "	aut.account,\n"
				+ " aut.tel,\n"
				+ "	CASE ert.`status`\n"
				+ "WHEN 0 THEN\n"
				+ "	'不通过'\n"
				+ "WHEN 1 THEN\n"
				+ "	'通过'\n"
				+ "WHEN 2 THEN\n"
				+ "	'待审核'\n"
				+ "END statusName\n"
				+ "FROM\n"
				+ "	exception_record_t ert\n"
				+ "LEFT JOIN exchange_electric_record_t eet ON ert.execChargid = eet.id\n"
				+ "AND ert.del = 0\n"
				+ "LEFT JOIN account_user_t aut ON aut.id = ert.accountid and  aut.del=0\n"
				+ "LEFT JOIN admin_user_t adut on adut.id=ert.operid and adut.del=0\n"
				+ "AND aut.del = 0\n" 
				+ "WHERE\n" + "	ert.del = 0\n"
				+ "AND ert.type = 0";

		sql = tradeAbnormalGeneralService.getwhereSQL(sql, paramList, sortfield);

		// 获取分页查询的数据结果
		totalRecord = tradeAbnormalGeneralService.getCount(sql, paramList);

		List<ExceptionRecordModel> privateInfoList = tradeAbnormalGeneralService
				.getList(ExceptionRecordModel.class, paramList, sql, pageIndex,
						15, sortfield);

		//是否为导出excel操作
		if("true".equals(request.getParameter("export"))){
			
			tradeAbnormalGeneralService.doDownloadAction(request, response,sql);
			
			return -1;
		}else {
			
			// 设置后台用户界面所需的其他数据
			tradeAbnormalGeneralService.setpersonListData(request, mv, pageIndex,
					totalRecord, privateInfoList);
			
			return 1;
		}
	
	}

	@SuppressWarnings("unchecked")
	public int companyListrecord(HttpServletRequest request,HttpServletResponse response, ModelAndView mv) {
		// 公司账户
		String accountid = request.getParameter("accountid");
		String begindate = request.getParameter("begindate");
		String enddate = request.getParameter("enddate");
		String status = request.getParameter("status");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "aut.id", "=", accountid };
		Object[] arr2 = { "ert.createdate", "d>", begindate };
		Object[] arr3 = { "ert.createdate", "d<", enddate };
		Object[] arr4 = { "ert.status", "=", status };
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		paramList.add(arr4);

		String sql ="SELECT\n" +
				"	ert.*, eet.serialNum,\n" +
				"	adut.username opername,\n" +
				"	aut.NAME accountName,\n" +
				"  aut.tel,\n" +
				"	aut.account,\n" +
				"	CASE ert.`status`\n" +
				"WHEN 0 THEN\n" +
				"	'不通过'\n" +
				"WHEN 1 THEN\n" +
				"	'通过'\n" +
				"WHEN 2 THEN\n" +
				"	'待审核'\n" +
				"END statusName\n" +
				"FROM\n" +
				"	exception_record_t ert\n" +
				"LEFT JOIN exchange_electric_record_t eet ON ert.execChargid = eet.id AND ert.del = 0\n" +
				"LEFT JOIN admin_user_t adut on adut.id=ert.operid and adut.del=0\n" +
				"LEFT JOIN car_account_t cat on cat.id=eet.carAccountid \n" +
				"LEFT JOIN  car_t ct on ct.id=cat.carid and ct.del=0\n" +
				"LEFT JOIN account_user_t aut ON aut.id = ct.companyid and  aut.del=0\n" +
				"AND aut.del = 0\n" +
				"WHERE\n" +
				"	ert.del = 0\n" +
				"AND ert.type = 1";

		sql = tradeAbnormalGeneralService.getwhereSQL(sql, paramList, sortfield);

		// 获取分页查询的数据结果
		totalRecord = tradeAbnormalGeneralService.getCount(sql, paramList);

		List<ExceptionRecordModel> companyInfoList = tradeAbnormalGeneralService
				.getList(ExceptionRecordModel.class, paramList, sql, pageIndex,
						15, sortfield);

		// 设置后台用户界面所需的其他数据
		tradeAbnormalGeneralService.setCompanyList(request, mv, pageIndex,
				totalRecord, companyInfoList);
		
		if("true".equals(request.getParameter("export"))){
			
			tradeAbnormalGeneralService.doDownloadAction(request, response,sql);
			return -1;
			
		}else {
			return 1;
		}
	
	}
	
	@RequestMapping(value = "del_trade_abnormal_general", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String delTradeAbnormal(HttpServletRequest request,
			String id) {

		String task = request.getParameter("task");
		tradeAbnormalGeneralService.updateDelById(id);

		return "redirect:/trade_abnormal_general?task="+task;
	}
	
	@RequestMapping(value = "get_person_byexchageid_general")
	@ResponseBody
	public ExceptionRecordModel getPersonByExchageid(HttpServletRequest request,
			String id) {
		
		ExceptionRecordModel exceptionRecordModel=tradeAbnormalGeneralService.getPersonByExchageid(id);

	    return exceptionRecordModel;
	}
	
	@RequestMapping(value = "get_company_byexchageid_general")
	@ResponseBody
	public ExceptionRecordModel getCompanyByExchageid(HttpServletRequest request,
			String id) {
		
	
		ExceptionRecordModel exceptionRecordModel=tradeAbnormalGeneralService.getCompanyByExchageid(id);

	    return exceptionRecordModel;
	}

	@RequestMapping(value = "add_trade_abnormal_general/{a}")
	@ResponseBody
	public   AjaxObj jsonAddPackagePostage(HttpServletRequest request,
			@PathVariable("a") int a ,ExceptionRecordModel exceptionRecordModel) {
 

		  AjaxObj ajaxObj = tradeAbnormalGeneralService
					.insertExceptionRecordModel(exceptionRecordModel);
 
		return ajaxObj;
	}
	
}